<template>
  <el-dialog
    :title="'为【' + titleName + '】分配权限'"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-tree
      v-if="bol"
      ref="tree"
      check-strictly
      node-key="id"
      show-checkbox
      :default-checked-keys="defaultChecked"
      :data="list"
      :props="{ label: 'name', childre: 'childre' }"
    />
    <!-- 默认选中：在以前选中基础上累加
      el-tree
             node-key="唯一标识字段名（大部分时候是id）"
            :default-checked-keys="['需要勾选项的id的值']"
       获取已勾选结果：this.$refs.tree.getCheckedKeys()
       设置勾选：this.$refs.tree.setCheckedKeys(['需要勾选的id集合'])
      万能还原组件方式：静默刷新
           用到上拉加载
    静默刷新：
       作用： 1：将组件还原到初始状态
             2：某数据值修改了，没有渲染，可通过它渲染
     -->

    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      titleName: '',
      list: [],
      defaultChecked: [],
      bol: true,
      id: ''
    }
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysPermission()
        this.list = changeData(res.data, '0')
        console.log('权限数据', res)
      }
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.defaultChecked = res.data.permIds
      console.log('角色详情', res)
    },
    // 还原tree为初始状态
    closeEvent() {
      this.defaultChecked = []
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
      //   this.$refs.tree.setCheckedKeys([])
    },
    // 确定点击
    async submit() {
      //   console.log(this.$refs.tree.getCheckedKeys())
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.isShow = false
    }
  }
}
</script>
<style></style>
